//
//  kWSimpleCell.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/6/2.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "kWSimpleCell.h"
#import "kWSimpleItem.h"
#import "kWHomeModel.h"

static NSString * const kW_simple_Id               = @"kW_simple_Identifier";
#define ItemView_H HEIGHT * 0.21

@interface kWSimpleCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)UICollectionView           *simpleView;
@property(nonatomic,strong)UICollectionViewFlowLayout *layout;

@end

@implementation kWSimpleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self buildSubViews];
    }
    return self;
}

- (void)buildSubViews
{
    [self.contentView addSubview:self.simpleView];
    
}

- (void)setData:(NSArray<kWHomeModel *> *)data
{
    _data = data;
}

#pragma mark datasource delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.data.count > 0 ? self.data.count : 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    kWSimpleItem*cell = [collectionView dequeueReusableCellWithReuseIdentifier:kW_simple_Id forIndexPath:indexPath];
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0,15,0,15);
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    kWSimpleItem *item = (kWSimpleItem *)cell;
    kWHomeModel *model = [[kWHomeModel alloc] initWithDictionary:(NSDictionary *)self.data[indexPath.item] error:nil];
    item.homeModel = model;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    kWHomeModel *model = [[kWHomeModel alloc] initWithDictionary:(NSDictionary *)self.data[indexPath.item] error:nil];
    if (self.simpleBlock) {
        self.simpleBlock(model);
    }
}


#pragma mark lazy
- (UICollectionViewFlowLayout *)layout
{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout  alloc] init];
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _layout.minimumLineSpacing = 10;
        _layout.minimumInteritemSpacing = 1;
        CGFloat itemW = WIDTH * 0.7;
        CGFloat itemH = ItemView_H -10;
        _layout.itemSize = CGSizeMake(itemW,itemH);
    }
    return _layout;
}

- (UICollectionView *)simpleView
{
    if (!_simpleView) {
        _simpleView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, WIDTH,ItemView_H -10) collectionViewLayout:self.layout];
        _simpleView.delegate = self;
        _simpleView.dataSource = self;
        _simpleView.bounces = false;
        _simpleView.backgroundColor = [UIColor whiteColor];
        _simpleView.showsHorizontalScrollIndicator = false;
        [_simpleView registerClass:[kWSimpleItem class] forCellWithReuseIdentifier:kW_simple_Id];
    }
    return _simpleView;
}




@end
